<template>
    <el-dialog :title="!deptData.deptId ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="deptData" :rules="deptRule" ref="deptData" @keyup.enter.native="dataFormSubmit()"
            label-width="80px">
            <el-form-item label="部门编码" prop="deptCode">
                <el-input v-model="deptData.deptCode" placeholder="部门编码"></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="deptName">
                <el-input v-model="deptData.deptName" placeholder="部门名称"></el-input>
            </el-form-item>
            <el-form-item label="上级学院" prop="parentId">
                <el-popover ref="parentName" placement="bottom-start" trigger="click">
                    <el-tree :data="deptList" :props="deptListTreeProps" node-key="deptId" ref="deptListTree"
                        @current-change="deptListTreeCurrentChangeHandle" :default-expand-all="true"
                        :highlight-current="true" :expand-on-click-node="false">
                    </el-tree>
                </el-popover>
                <el-input v-model="deptData.parentName" v-popover:parentName :readonly="true" placeholder="点击选择上级学院"
                    class="menu-list__input"></el-input>
            </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { deptApi } from '@/api/baseinfo';
import { clone } from '@/utils';

export default {
    components: {},
    props: {},
    data() {
        return {
            visible: false,
            deptData: {
                parentId: '',
                deptCode: '',
                deptName: '',
                parentName: ''
            },
            deptRule: {
                deptCode: { required: true, message: '部门编号不能为空', trigger: 'blur' },
                deptName: [
                    { required: true, message: '部门名称不能为空', trigger: 'blur' }
                ]
            },
            deptList: [],
            deptListTreeProps: {
                label: 'deptName',
                children: 'list'
            }

        };
    },
    watch: {},
    computed: {},
    methods: {
        init(id) {

            deptApi.getdeptListTree().then(({ data }) => {
                this.visible = true
                this.$nextTick(() => {
                    this.deptData = Object.assign({}, this.$options.data().deptData)
                    this.$refs['deptData'].clearValidate()
                })
                this.deptList = data
            }).then(() => {

                if (id) {
                    this.deptData.deptId = id
                    deptApi.getDeptInfo(id).then(({ dept }) => {
                        this.deptData = dept
                        let list = this.deptList
                        for (let deptItem of list) {
                            if (dept.parentId == deptItem.deptId) {
                                dept.parentName = deptItem.deptName
                            }
                        }
                    })
                }


            })


        },
        deptListTreeCurrentChangeHandle(data, node) {
            this.deptData.parentId = data.deptId
            this.$nextTick(() => {
                this.deptData.parentName = data.deptName
            })

        },
        menuListTreeSetCurrentNode() {
            this.$refs.deptListTree.setCurrentKey(this.dataForm.parentId)
            this.dataForm.parentId = (this.$refs.deptListTree.getCurrentNode() || {})['name']
        },
        dataFormSubmit() {
            this.$refs['deptData'].validate((valid) => {
                if (valid) {
                    deptApi.saveOrUpdate(this.deptData, this.deptData.deptId ? 'update' : 'save').then(() => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.visible = false
                                this.$emit('refreshDataList')
                            }
                        })

                    })
                }
            })


        }
    },
    created() { },
    mounted() { },

};
</script>

<style lang="scss">

</style>

<style lang="scss" scoped>

</style>
